package com.xc.freeapp.account.controller;



import java.util.List;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.entity.FreeRegPhone;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.FreeregphoneService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.DateUtils;
import com.xc.freeapp.util.TokenAnnotation;


@Controller
@RequestMapping("/freeregphone")
@Api(value="/freeregphone",description = "免费通话")
public class FreeregphoneController extends AbstractController {

	private Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private FreeregphoneService freeregphoneservice;
	
	@TokenAnnotation(required=false)
	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "保存免费通话",response=FreeRegPhone.class)
	public FreeRegPhone savafreereg(@RequestBody @Valid FreeRegPhone freereg)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		if("1".equals(freereg.getType())){
			freereg.setRegdatetime(DateUtils.getDate());
		}
		freereg.setUserid(authInfo.getUserIntId());
		freereg.setAppid(authInfo.getAppIntId());
		freereg.setCreateby(authInfo.getUserIntId());
		freereg.setUpdatetime(DateUtils.getDate());
		freereg.setCreatetime(DateUtils.getDate());
		freereg.setUpdateby(authInfo.getUserIntId());
		freeregphoneservice.insert(freereg);
		FreeRegPhone freeregphone = freeregphoneservice
				.selectByPrimaryKey(freereg.getId());
		return freeregphone;
	}
	@TokenAnnotation(required=false)
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取免费通话",response=FreeRegPhone.class)
	public List<FreeRegPhone> getfreereg()
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		
		
		return freeregphoneservice.selectByuserid(authInfo.getUserIntId());
	}
}
